package com.wad.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.wad.dto.system.AssginMenuDto;
import com.wad.entity.system.SysMenu;
import com.wad.manager.mapper.SysRoleMenuMapper;
import com.wad.manager.service.SysMenuService;
import com.wad.manager.service.SysRoleMenuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wad
 * @date 2023/12/04 14:20
 * @project e-commerce
 **/
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {


    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    private SysMenuService sysMenuService;

    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {

        Map<String,Object> map=new HashMap<>();
        //先查询所有的菜单
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        //查询角色分配过的列表
        List<Long> roleMenuIds=sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
        map.put("sysMenuList",sysMenuList);
        map.put("roleMenuIds",roleMenuIds);
        return map;
    }


    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //删除角色之前分配过的菜单
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());

        //保存分配的数据
        List<Map<String, Number>> menuInfo = assginMenuDto.getMenuIdList();
        if (CollectionUtil.isNotEmpty(menuInfo)){
            sysRoleMenuMapper.doAssign(assginMenuDto);
        }
    }
}
